search.onclick = function () {
    var option = county.getElementsByTagName('option');
    var weatherCode;
    for (var i = 0, length = option.length; i < length; i++) {
        if (option[i].selected) {
            weatherCode = option[i].getAttribute('weatherCode');
        }
    }
    ajax({
        url: "/getWeather",
        data: { weatherCode: weatherCode },
        dataType: "json",
        type: 'post',
        success: function (res) {
            var { error_code, msg } = res
            if (error_code != 0) {
                alert(msg)
                return
            }
            var { data: { weatherinfo } } = res
            var html = "<b>城市：</b>" + weatherinfo.city + "<br><b>温度：</b>" + weatherinfo.temp + "度<br><b>风向：</b>" + weatherinfo.WD + "<br><b>风速：</b>" + weatherinfo.WS + "<br><b>湿度：</b>" + weatherinfo.SD + "<br>";
            result.innerHTML = html;
        }
    });
}

function getData(pid = 0, cid = 0) {
    ajax({
        url: "/china.xml",
        dataType: "xml",
        type: "get",
        success: function (res) {
            var china = res.getElementsByTagName('China')[0];
            var provinceData = china.children;
            for (var i = 0, length = provinceData.length; i < length; i++) {
                var provinceId = provinceData[i].getAttribute('id');
                if (pid != 0) {
                    if (pid == provinceId) {
                        var cityData = provinceData[i].children;
                        var cityOption = '<option value="0">请选择市</option>';
                        for (var j = 0, clength = cityData.length; j < clength; j++) {
                            var cityId = cityData[j].getAttribute('id');
                            if (pid != 0 && cid != 0) {
                                if (cid == cityId) {
                                    var countryData = cityData[j].children;
                                    var countryOption = '<option value="0">请选择县/区</option>';
                                    for (var h = 0, cclength = countryData.length; h < cclength; h++) {
                                        var countryId = countryData[h].getAttribute('id');
                                        var countryName = countryData[h].getAttribute('name');
                                        var weatherCode = countryData[h].getAttribute('weatherCode');
                                        countryOption += '<option value="' + countryId + '" weatherCode="' + weatherCode + '">' + countryName + '</option>';
                                    }
                                    county.innerHTML = countryOption;
                                }
                            } else {
                                var cityName = cityData[j].getAttribute('name');
                                cityOption += '<option value="' + cityId + '">' + cityName + '</option>';
                                city.innerHTML = cityOption;
                            }
                        }
                    }
                } else {
                    var provinceName = provinceData[i].getAttribute('name');
                    province.innerHTML += '<option value="' + provinceId + '">' + provinceName + '</option>';
                }
            }
        }
    });
}

getData();
province.onchange = function () {
    var pid = this.value;
    getData(pid);
}
city.onchange = function () {
    var pid = province.value;
    var cid = this.value;
    getData(pid, cid);
}
